import { AfterViewInit, ChangeDetectorRef, Component, OnChanges, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema, SFSchemaEnumType, SFUISchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { EAEnvironmentService } from '@shared';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { of } from 'rxjs';
import { delay, map } from 'rxjs/operators';
import { SystemService } from '../../../services/system.service';
import { SettingMenuComponent } from '../menu/menu.component';

@Component({
  selector: 'app-cuc-edit',
  templateUrl: './edit.component.html',
  styleUrls: ['./edit.less']
})
export class SettingRoleEditComponent implements OnInit {
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @ViewChild('menu', { static: false })
  menu!: SettingMenuComponent;
  roleInfoData: any = {};
  authorityAssistId: any[] = [];
  params: any;
  changeValue: boolean = false;
  schema!: SFSchema;
  authority: any[] = [];
  constructor(private modal: NzModalRef, public service: SystemService) {}

  ngOnInit(): void {
    this.initSF();
    if (this.params.id) {
      this.getRoleInfo();
    }
  }
  initSF() {
    this.schema = {
      properties: {
        roleName: {
          title: '角色名称',
          type: 'string',
          default: this.roleInfoData.roleName,
          maxLength: 20,
          ui: {
            placeholder: '请输入角色名称'
          }
        },
        roleDescription: {
          title: '角色描述',
          type: 'string',
          maxLength: 50,
          default: this.roleInfoData.roleDescription,
          ui: {
            autosize: { minRows: 3 },
            hidden: this.params.lookType === 'detail',
            placeholder: '请输入角色描述',
            widget: 'textarea'
          }
        }
      },
      required: ['roleName']
    };
  }

  getRoleInfo() {
    this.roleInfoData = [];
    const params = {
      id: this.params.id
    };
    console.log(this.params.infoUrl);
    this.service.request(this.params.infoUrl, params).subscribe(res => {
      if (res) {
        this.roleInfoData = res;
        this.initSF();
      }
    });
  }
  getData(res: { authority: any[]; authorityAssistId: any[] }) {
    console.log('修改了');

    this.authority = res.authority;
    this.authorityAssistId = res.authorityAssistId;
  }
  close() {
    this.modal.destroy();
  }
  changeIF(value: any) {
    console.log(value);
    console.log('54545');
    this.changeValue = true;
  }
  sure() {
    if (!this.sf?.valid) {
      this.service.msgSrv.warning('角色名称不能为空');
      return;
    }
    console.log(this.changeValue);

    const auths = this.menu?.washTree();
    if (auths.authorityAssistId.length === 0) {
      this.service.msgSrv.warning('请选择权限!');
      return;
    }
    const params: any = {
      id: this.params.id,
      ...this.sf?.value,
      authority: auths.authority,
      authorityAssistId: auths.authorityAssistId
    };

    if (this.params.id === 0) {
      delete params.id;
    }
    if (this.params?.type === 'freight') {
      Object.assign(params, { enterpriseId: 0, enterpriseProjectId: 0 });
    }
   

    if (this.params.id) {
      this.service.request(this.params.updateUrl, params).subscribe(res => {
        if (res) {
          this.service.msgSrv.success('编辑成功');
          this.modal.close(true);
        }
      });
    } else {
      this.service.request(this.params.addUrl, params).subscribe(res => {
        if (res) {
          this.service.msgSrv.success('新增成功');
          this.modal.close(true);
        }
      });
    }
  }
}
